package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * ClassName: ItemServiceImpl
 * Package: com.atguigu.gmall.item.service.impl
 * Description:
 *
 * @Author 刘轩
 * @Create 2023-08-22 9:53
 * @Version 1.0
 */

@Service
public class ItemServiceImpl implements ItemService {

    /*
   汇总数据：
    */
    @Qualifier("com.atguigu.gmall.product.client.ProductFeignClient")

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Qualifier("com.atguigu.gmall.list.client.ListFeignClient")
    @Autowired
    private ListFeignClient listFeignClient;
    @Override
    public Map getItem(Long skuId) {

        //  创建map 集合来存储商品详情页面需要的数据
        Map map = new HashMap();

        // 判断布隆过滤器中是否存在该商品
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
        if (!bloomFilter.contains(skuId)) {
            return map;
        }

        //  远程调用 获取skuInfo 数据
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            return skuInfo;
        },threadPoolExecutor);


        //  查询分类数据
        CompletableFuture<Void> viewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", categoryView);
        },threadPoolExecutor);


        //  查询最新价格：
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            map.put("price", skuPrice);
        },threadPoolExecutor);

        //  获取海报信息
        CompletableFuture<Void> postercompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo -> {
            List<SpuPoster> spuPosterList = productFeignClient.getSpuPosterBySpuId(skuInfo.getSpuId());
            map.put("spuPosterList", spuPosterList);
        }));
        //  根据skuId 获取平台属性数据
        CompletableFuture<Void> attrCompletableFuture = CompletableFuture.runAsync(() -> {
            List<BaseAttrInfo> attrList = productFeignClient.getAttrList(skuId);
            //  只需要attrName, attrValue; 不能直接存储.
            //  第一种方式：修改页面
            //  第二种方式：修改后台代码 skuAttr.attrName; skuAttr.attrValue ----> skuAttr 是实体类 / 或者是map
            List<HashMap<String, String>> mapList = attrList.stream().map(baseAttrInfo -> {
                //  声明一个map 集合
                HashMap<String, String> hashMap = new HashMap<>();
                //  attrName = baseAttrInfo.getAttrName();
                //  attrValue =  baseAttrInfo.getAttrValueList().get(0).getValueName();
                hashMap.put("attrName", baseAttrInfo.getAttrName());
                hashMap.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
                return hashMap;
            }).collect(Collectors.toList());
            map.put("skuAttrList", mapList);
        },threadPoolExecutor);


        //  获取销售属性
        CompletableFuture<Void> spuSaleAttrCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            map.put("spuSaleAttrList", spuSaleAttrList);
        },threadPoolExecutor);

        //  获取切换功能数据.
        CompletableFuture<Void> skuJsoncompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map skuValueIdsMap = this.productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String jsonStr = JSON.toJSONString(skuValueIdsMap);
            map.put("valuesSkuJson", jsonStr);
        },threadPoolExecutor);


        //  调用热度排名方法.
        CompletableFuture<Void> hotScoreCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        },threadPoolExecutor);

// 多任务组合：
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                viewCompletableFuture,
                spuSaleAttrCompletableFuture,
                postercompletableFuture,
                priceCompletableFuture,
                skuJsoncompletableFuture,
                attrCompletableFuture,
                hotScoreCompletableFuture

        ).join();





        //  返回map 集合数据
        return map;
    }
}
